package com.be.beadmin.product.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.product.dto.input.PlantCapacityCalendarDto;
import com.be.beadmin.product.dto.output.PlantCapacityCalendarEx;
import com.be.beadmin.product.service.PlantCapacityCalendarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author zjm
* @date 2023-03-26
**/
@Api(tags = "产能负荷")
@RestController
public class PlantCapacityCalendarController {

    @Autowired
    private  PlantCapacityCalendarService plantCapacityCalendarService;

    /**
     * 查询产能日历，   WorkHour == null  说明是实际产能的     RealWorkHour == null 说明是计划产能
     * @param pageEntity
     * @return
     */
    @ApiOperation("获取产能日历数据")
    @PostMapping(value = "/product/getPlantCapacityCalendarByPage")
    public ResultModel getPlantCapacityCalendarByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(plantCapacityCalendarService.getPlantCapacityCalendarByPage(pageEntity)) ;
    }



    @ApiOperation("获取产能日历数据 - 按照批次统计")
    @PostMapping(value = "/product/getPlantCapacityCalendarByPageAndBatch")
    public ResultModel getPlantCapacityCalendarByPageAndBatch(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(plantCapacityCalendarService.getPlantCapacityCalendarByPageAndBatch(pageEntity)) ;
    }

    @ApiOperation("按照批次统计产能日历数据")
    @PostMapping(value = "/product/getPlantCapacityCalendarByBatch")
    public ResultModel getPlantCapacityCalendarByBatch()  {
        return ResultModel.success(plantCapacityCalendarService.getCapacityByBatch()) ;
    }

//    @PostMapping(value = "/product/getPlantCapacityCalendarByPage")
//    public ResultModel getPlantCapacityCalendarByPage(@RequestBody PageEntity pageEntity)  {
//        return ResultModel.success(plantCapacityCalendarService.getPlantCapacityCalendarByPage(pageEntity)) ;
//    }

    @PostMapping("/product/addPlantCapacityCalendar")
    public ResultModel addPlantCapacityCalendar(@RequestBody PlantCapacityCalendarDto plantCapacityCalendarDto){
        plantCapacityCalendarService.addPlantCapacityCalendar(plantCapacityCalendarDto);
        return ResultModel.success(ResultModel.ADD);
    }

    //    @PostMapping("/product/computeCapacitySumByDayAndRange")
    //    public ResultModel computeCapacitySumByDayAndRange(@RequestBody PlantCapacityCalendarDto plantCapacityCalendarDto){
    //        plantCapacityCalendarService.computeCapacitySumByDayAndRange(date, range, process);
    //        return ResultModel.success(ResultModel.ADD);
    //    }


    @PostMapping("/product/delPlantCapacityCalendar")
    public ResultModel delPlantCapacityCalendar(@RequestBody PlantCapacityCalendarDto plantCapacityCalendarDto){
        plantCapacityCalendarService.delPlantCapacityCalendar(plantCapacityCalendarDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/product/updatePlantCapacityCalendar")
    public ResultModel updatePlantCapacityCalendar(@RequestBody PlantCapacityCalendarDto plantCapacityCalendarDto){
        plantCapacityCalendarService.updatePlantCapacityCalendar(plantCapacityCalendarDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/product/exportPlantCapacityCalendar")
    public void exportPlantCapacityCalendar(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<PlantCapacityCalendarEx> plantCapacityCalendarPage = plantCapacityCalendarService.getPlantCapacityCalendarByPage(pageEntity);
        List<PlantCapacityCalendarEx> plantCapacityCalendarExes = plantCapacityCalendarPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("PlantCapacityCalendar接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), PlantCapacityCalendarEx.class).sheet("sheet1").doWrite(plantCapacityCalendarExes);
    }

    @ApiOperation("根据柜体编码获取工时对比信息")
    @PostMapping("/product/getWorkingHoursComparisonsByProductCode")
    public ResultModel getWorkingHoursComparisonsByProductCode(@RequestParam String productCode){
        return ResultModel.success("success" , plantCapacityCalendarService.getWorkingHoursComparisonsByProductCode(productCode));
    }
}